package com.taomu.app.zongchouhuli.moudle.mine.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.moudle.mine.entity.PayWayEntity;

import java.util.ArrayList;
import java.util.List;

public class RechargePayWayAdp extends RecyclerView.Adapter<RechargePayWayAdp.RechargePayWayHolder>{

    private List<PayWayEntity> mData;

    private List<PayWayEntity> mSelectData;

    private Context mContext;

    private LayoutInflater inflater;

    public RechargePayWayAdp(Context mContext){
        this.mContext = mContext;
        this.mData = new ArrayList<>();
        this.mSelectData = new ArrayList<>();
        this.inflater = LayoutInflater.from(mContext);
    }

    public void setData(List<PayWayEntity> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        if(mData.size() > 0){
            mSelectData.add(mData.get(0));
        }
        notifyDataSetChanged();
    }

    public void addData(List<PayWayEntity> mData){
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public RechargePayWayHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_payway,parent,false);
        return new RechargePayWayHolder(view);
    }

    @Override
    public void onBindViewHolder(RechargePayWayHolder holder, int position) {
        final PayWayEntity entity = mData.get(position);
        holder.update(entity);
        holder.mCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkData(entity);
                if(listener != null){
                    listener.onItemClick(entity);
                }
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkData(entity);
                if(listener != null){
                    listener.onItemClick(entity);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class RechargePayWayHolder extends RecyclerView.ViewHolder{

        private TextView mTitleTv;

        private CheckBox mCheckBox;

        public RechargePayWayHolder(View itemView) {
            super(itemView);
            mTitleTv = itemView.findViewById(R.id.item_pay_name);
            mCheckBox = itemView.findViewById(R.id.item_payway_checkbox);
        }

        public void update(PayWayEntity entity){
            mTitleTv.setText(entity.getTitle());
            if(isSelect(entity)){
                mCheckBox.setChecked(true);
                mTitleTv.setTextColor(mContext.getResources().getColor(R.color.base_blue));
            }else{
                mCheckBox.setChecked(false);
                mTitleTv.setTextColor(mContext.getResources().getColor(R.color.text_normal_black));
            }
        }
    }

    public PayWayEntity getSelect(){
        if(mSelectData.size()>=1){
            return mSelectData.get(0);
        }
        return null;
    }

    private void checkData(PayWayEntity entity){
        mSelectData.clear();
        mSelectData.add(entity);
        notifyDataSetChanged();
    }

    private boolean isSelect(PayWayEntity entity){
        return mSelectData.contains(entity);
    }

    private RechargePayWayListener listener;

    public void setRechargePayWayListener(RechargePayWayListener listener){
        this.listener = listener;
    }

    public interface RechargePayWayListener{
        void onItemClick(PayWayEntity entity);
    }
}
